﻿using System;
using System.Drawing.Imaging;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.Extensions;
using OpenQA.Selenium.Support.PageObjects;
using Secured3d.Auto.Helpers;
using Secured3d.Auto.Utils;

namespace Secured3d.Auto.Pages
{
    public class UserProfilePage : BasePage
    {
        // Locators
        [FindsBy(How = How.Id, Using = "new_printer_prev_block")] 
        private IWebElement popUpWindow = null;

        [FindsBy(How = How.CssSelector, Using = "input.close_prev_block")] 
        private IWebElement usingCloudButton = null;

        [FindsBy(How = How.CssSelector, Using = "#new_printer_prev_block input.close_prev_block")] 
        private IWebElement closePopupWindowButton = null;

        private Menu menu;
        private PrintersBlock printers;
        private UploadBlock upload;
        private MyFilesBlock myFiles;

        public Menu Menu 
        {
            get
            {
                menu = menu ?? new Menu(); 
                return menu; 
            }
        }

        public PrintersBlock Printers
        {
            get
            {
                printers = printers ?? new PrintersBlock();
                return printers;
            }
        }

        public UploadBlock UploadFile
        {
            get
            {
                upload = upload ?? new UploadBlock();
                return upload;
            }
        }

        public MyFilesBlock MyFiles
        {
            get
            {
                myFiles = myFiles ?? new MyFilesBlock();
                return myFiles;
            }
        }
        
        public bool IsRegistrationSuccessfull
        {
            get
            {
                var result = false;
                try
                {
                    if (popUpWindow.WaitUntilVisible().Displayed)
                    {
                        closePopupWindowButton.Click();
                        result = true;
                    }

                }
                catch (ElementNotVisibleException exception)
                {
                    var timeStamp = DateTime.Now.ToString("yyyy-MM-dd-hhmm-ss");
                    Driver.TakeScreenshot()
                        .SaveAsFile("C:\\TestResults\\Exception-" + timeStamp + ".png", ImageFormat.Png);
                }

                return result;

            }
        }

        public bool IsAt(string userName)
        {
            try
            {
                return Menu.AuthorizedUser.Contains(userName.ToUpper());
            }
            catch (TimeoutException exception){}

            throw new Exception("Can't verify authorized user name.");
        }

        public void ClosePopUpWindow()
        {
            usingCloudButton.Click();
        }
        
    }
}
